package org.example.socket网络编程.TCP;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

// 客户端
public class TCPClientDemo {
    public static void main(String[] args){
        Socket socket = null;
        OutputStream outputStream = null;
        try{
            InetAddress byName = InetAddress.getByName("localhost");
            socket = new Socket(byName, 9999);
            outputStream = socket.getOutputStream();
            outputStream.write("oh om oh".getBytes());
        }catch (IOException e){
            throw new RuntimeException(e);
        }finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
